import { useAddKey } from '@/hooks/useAddKey';
import { message } from 'antd';
import {
  getmanagement,
  getrental,
  getshopsale,
  getshoprental,
} from '@/api/index';
const kefu = {
  namespace: 'business',
  state: {
    //出售管理数据
    listData: [],
    //出租管理数据
    rentalList: [],
    //出售管理数据
    shopListData: [],
    searchList: [],
    //出租管理数据
    shopRentalList: [],
  },
  subscriptions: {
    INITHOUSE({ dispatch }) {
      dispatch({
        type: 'INITDATA',
      });
    },
    INITRENTAL({ dispatch }) {
      dispatch({
        type: 'INITRENTAL1',
      });
    },
    INITSHOPMANAGEMENT({ dispatch }) {
      dispatch({
        type: 'SHOPMANAGEMENT',
      });
    },
    INITSHOPRENTAL({ dispatch }) {
      dispatch({
        type: 'INITSHOPRENTAL1',
      });
    },
  },
  effects: {
    *INITDATA(action, { call, put }) {
      const data = yield call(getmanagement);
      yield put({ type: 'INIT', payload: data.data.RECORDS });
    },
    *INITRENTAL1(action, { call, put }) {
      const data = yield call(getrental);
      yield put({ type: 'INITRENTAL2', payload: data.data.RECORDS });
    },
    *SHOPMANAGEMENT(action, { call, put }) {
      const data = yield call(getshopsale);
      yield put({ type: 'SHOPMANAGEMENT1', payload: data.data.RECORDS });
    },
    *INITSHOPRENTAL1(action, { call, put }) {
      const data = yield call(getshoprental);
      yield put({ type: 'INITSHOPRENATL', payload: data.data.RECORDS });
    },
  },
  reducers: {
    //编辑出售管理
    EDITBUILDMANAGEMENTLDETAIL(state, { data }) {
      let newState = JSON.parse(JSON.stringify(state));
      newState.listData.forEach((item) => {
        if (item.id == data.id) {
          item.estate_name = data.estate_name;
          return item;
        }
      });
      return { ...newState };
    },
    //编辑出租管理
    EDITBUILDRENTAL(state, { data }) {
      let newState = JSON.parse(JSON.stringify(state));
      newState.rentalList.forEach((item) => {
        if (item.id == data.id) {
          item.estate_name = data.estate_name;
          return item;
        }
      });
      return { ...newState };
    },
    //删除出售管理数据
    DELMANAGEMENT(state, { payload }) {
      let newState = JSON.parse(JSON.stringify(state));
      newState.listData.forEach((item, index) => {
        if (item.id == payload) {
          if (confirm('是否删除')) {
            newState.listData.splice(index, 1);
            message.success('删除成功');
          }
        }
      });
      return { ...newState };
    },
    //删除出租管理数据
    DELRENTAL(state, { payload }) {
      let newState = JSON.parse(JSON.stringify(state));
      newState.rentalList.forEach((item, index) => {
        if (item.id == payload) {
          if (confirm('是否删除')) {
            newState.rentalList.splice(index, 1);
            message.success('删除成功');
          }
        }
      });
      return { ...newState };
    },
    //编辑出售管理
    EDITSHOPMANAGEMENTLDETAIL(state, { data }) {
      let newState = JSON.parse(JSON.stringify(state));
      newState.shopListData.forEach((item) => {
        if (item.id == data.id) {
          item.estate_name = data.estate_name;
          return item;
        }
      });
      return { ...newState };
    },
    //编辑出租管理
    EDITSHOPRENTAL(state, { data }) {
      let newState = JSON.parse(JSON.stringify(state));
      newState.shopRentalList.forEach((item) => {
        if (item.id == data.id) {
          item.estate_name = data.estate_name;
          return item;
        }
      });
      return { ...newState };
    },
    //删除出售管理数据
    DELSHOPMANAGEMENT(state, { payload }) {
      let newState = JSON.parse(JSON.stringify(state));
      newState.shopListData.forEach((item, index) => {
        if (item.id == payload) {
          if (confirm('是否删除')) {
            newState.shopListData.splice(index, 1);
            message.success('删除成功');
          }
        }
      });
      return { ...newState };
    },
    //删除出租管理数据
    DELSHOPRENTAL(state, { payload }) {
      let newState = JSON.parse(JSON.stringify(state));
      newState.shopRentalList.forEach((item, index) => {
        if (item.id == payload) {
          if (confirm('是否删除')) {
            newState.shopRentalList.splice(index, 1);
            message.success('删除成功');
          }
        }
      });
      return { ...newState };
    },
    INITRENTAL2(state, { payload }) {
      let newState = JSON.parse(JSON.stringify(state));
      newState.rentalList = useAddKey(payload);
      localStorage.setItem('rentalList', JSON.stringify(newState.rentalList));
      return {
        ...newState,
      };
    },
    INIT(state, { payload }) {
      let newState = JSON.parse(JSON.stringify(state));
      newState.listData = useAddKey(payload);
      newState.listData = newState.listData.reverse();
      localStorage.setItem('listData', JSON.stringify(newState.listDatat));
      return {
        ...newState,
      };
    },
    SEARCHLIST(state, { payload }) {
      console.log(payload);
      let newState = JSON.parse(JSON.stringify(state));
      newState.searchList = [...newState.rentalList];
      return {
        ...newState,
      };
    },
    SHOPMANAGEMENT1(state, { payload }) {
      let newState = JSON.parse(JSON.stringify(state));
      newState.shopListData = useAddKey(payload);
      return {
        ...newState,
      };
    },
    INITSHOPRENATL(state, { payload }) {
      console.log(payload);
      let newState = JSON.parse(JSON.stringify(state));
      newState.shopRentalList = useAddKey(payload);
      return {
        ...newState,
      };
    },
    SEARCH(state, { payload }) {
      console.log(payload);
      let newState = JSON.parse(JSON.stringify(state));
      switch (payload.count) {
        case 0:
          newState.listData = JSON.parse(localStorage.getItem('listData'));
          newState.listData = newState.listData.filter((item) => {
            if (item.title.includes(payload.title)) {
              return item;
            }
          });

          return {
            ...newState,
          };
        case 1:
          newState.rentalList = JSON.parse(localStorage.getItem('rentalList'));
          newState.rentalList = newState.rentalList.filter((item) => {
            if (item.title.includes(payload.title)) {
              return item;
            }
          });
          return {
            ...newState,
          };
        case 2:
          newState.searchList = [...newState.shopListData];
          newState.searchList = newState.searchList.filter((item) => {
            if (
              item.status === (payload.status === '正常' ? 1 : 0) &&
              item.title.includes(payload.title)
            ) {
              return item;
            }
          });
          return {
            ...newState,
          };
        case 3:
          newState.searchList = [...newState.shopRentalList];
          newState.searchList = newState.searchList.filter((item) => {
            if (
              item.status === (payload.status === '正常' ? 1 : 0) &&
              item.title.includes(payload.title)
            ) {
              return item;
            }
          });
          return {
            ...newState,
          };
        default:
          break;
      }
    },
    ADDSHOP(state, { data }) {
      let newState = JSON.parse(JSON.stringify(state));
      console.log(data);
      function add(arr) {
        arr.push({
          address: data.address,
          agent_id: 0,
          broker_id: 0,
          city: 4,
          complate_time: 0,
          complate_time_memo: null,
          create_time: 1522579131,
          developer_id: 222,
          developer_name: null,
          discount: null,
          hits: 607,
          id: arr[arr.length - 1].id + 1,
          img: '/uploads/house/20181102/6f367596a4cffa4ff1e94a8bf6172a98.png',
          is_discount: 0,
          key: arr[arr.length - 1].id + 1,
          lat: '20.021263625269',
          license_key: null,
          lng: '110.30991946701',
          opening_time: 0,
          opening_time_memo: null,
          ordid: 1000,
          estate_name: data.estate_name,
          pano_url: 'http://www.njtxfc.com/njpano/bgyfhc/tour.html',
          price: 22000,
          ratio: 83.3,
          rec_position: 0,
          red_packet: 0,
          renovation: 0,
          sale_address: null,
          sale_phone: '{"phone":"","extension":""}',
          sale_status: 1,
          status: 0,
          tags_id: '0',
          title: data.title,
          type_id: '0',
          unit: 1,
          update_time: new Date().getMinutes(),
          video: null,
          voice: 1,
          voiceurl: null,
        });
      }
      switch (data.count) {
        case 0:
          add(newState.listData);
          break;
        case 1:
          add(newState.rentalList);
          break;
        case 2:
          add(newState.shopListData);
          break;
        case 3:
          add(newState.shopRentalList);
          break;

        default:
          break;
      }

      return { ...newState };
    },
  },
};

export default kefu;
